﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TSV.Models;
using TSV.Library.Common;
using System.Data.Common;

namespace TSV.Areas.Administration.Models
{
    public class EmailRegisterDao: BaseDao
    {
        #region "Get"
        public List<sp_GetEmailResult> GetList()
        {
            return dbContext.sp_GetEmail().ToList();

        }

        public Email GetById(string id)
        {
            return dbContext.Emails.Where(c => c.ID.ToString().Equals(id)).SingleOrDefault<Email>();
        }
        public List<sp_GetEmailResult> Sort(List<sp_GetEmailResult> list, string sortColumn, string sortOrder)
        {
            int order;

            if (sortOrder == "desc")
            {
                order = -1;
            }
            else
            {
                order = 1;
            }
            switch (sortColumn)
            {

                case "Email":
                    list.Sort(
                         delegate(sp_GetEmailResult m1, sp_GetEmailResult m2)
                         { return m1.Email.CompareTo(m2.Email) * order; });
                    break;
                

            }

            return list;
        }
        #endregion
        public Message DeleteList(string ids)
        {
            Message msg = null;
            DbTransaction trans = null;
            bool canDelete = true;
            try
            {
                dbContext.Connection.Open();
                trans = dbContext.Connection.BeginTransaction();
                dbContext.Transaction = trans;

                if (!string.IsNullOrEmpty(ids))
                {
                    ids = ids.TrimEnd(',');
                    string[] idArr = ids.Split(',');
                    foreach (string id in idArr)
                    {

                        Email oscObj = GetById(id);
                        if (oscObj != null)
                        {
                            dbContext.Emails.DeleteOnSubmit(oscObj);
                            dbContext.SubmitChanges();

                        }
                    }
                    if (canDelete)
                    {
                        // Show succes message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, idArr.Count().ToString() + " Record(s)", "xóa");
                        trans.Commit();
                    }
                    else
                    {
                        trans.Rollback();
                    }
                }
            }
            catch (Exception)
            {
                if (trans != null) trans.Rollback();
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }
        public Email CheckEmailExits(string email)
        {
            return dbContext.Emails.Where(q => q.Email1 == email).FirstOrDefault();
        }
        public Message Update(Email objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Get current group in dbContext
                    Email objDb = GetById(objUI.ID.ToString());

                    if (objDb != null)
                    {
                        // Update info by objUI
                        objDb.Email1 = objUI.Email1;
                        // Submit changes to dbContext
                        dbContext.SubmitChanges();
                        // Show success message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, "Email đăng ký '" + objUI.Email1 + "'", "cập nhật");

                    }
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }

            return msg;
        }
        public Message Insert(Email objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Set more info                    

                    dbContext.Emails.InsertOnSubmit(objUI);
                    dbContext.SubmitChanges();

                    // Show success message
                    msg = new Message(MessageConstants.I0001, MessageType.Info, "Một email nhận bản tin", "thêm");
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }
    }
}